import Taro from '@tarojs/taro';
import { Text, View } from '@tarojs/components';
import { Cell, CellGroup, Icon } from '@nutui/nutui-react-taro';
import TaskItem from '../TaskItem/TaskItem.jsx';
import { useDispatch } from 'react-redux';
import { setEditTask, toggleEdit, deleteTask } from '../../store/taskSlice.js';
import './TaskCard.scss'

export default function TaskCard(props) {
    const { list, type } = props;
    const dispatch = useDispatch();

    const editNowTask = function(item) {
      dispatch(setEditTask({
        id: item.id
      }));
      dispatch(toggleEdit());
    }

    /**
     * 删除当前任务
     * @param {Object} item 
     */
    const deleteTaskItem = function(item) {
      Taro.showModal({
        title: '温馨提示',
        content: '确认要删除当前任务么？删除不可恢复！',
        success: function (res) {
          if (res.confirm) {
            dispatch(deleteTask({
              id: item.id
            }));
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    return (
      <CellGroup title={type}>
        {list && list.length > 0 ? list.map((item) => {
          return(
            <TaskItem item={item} key={item.id} />
          )
        }) : <Text className='task-nodata'>暂无数据...</Text>}
      </CellGroup>
    )
}
